package com.voxel.sense.infomsg.view.progress;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;


import com.example.com.common.utils.UIUtil;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.data.bean.ProgressBean;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ChenYiYao
 * @date 2018/5/14
 * des : 自定义多区间progress。
 */
public class ScaleProgressView extends View {


    private List<ProgressBean> Intervals;
    private String[] mPositionDesc = {"00:00", "04:00", "08:00", "12:00", "16:00"
            , "20:00", "24:00"};

    private Paint mProbgPaint;
    private Paint mProPaint;
    private Paint mSegmentPaint;
    private Paint mMarkerPaint; //底部刻度画笔

    private int mProgressColor; //绘制主颜色
    private int mCuttrnyColor; //绘制主颜色
    private int mtextColor; //绘制主颜色
    private int mSegmentColor; //绘制主颜色

    private int mMaxValue; //最大值
    private int mCurrentValue; //当前进度值
    private int mProgressHeight; //圆角 边框

    private int mRightX;
    private int mLeftX;
    private int mLeftY;
    private int mRightY;
    //左右两边默认预留一点空间
    private int mProgressLftPadding = 2;
    private int mProgresssRightPadding = 2;

    List<? extends ISegmentProgress> mSegmentProgressList;

    public ScaleProgressView(Context context) {
        this(context, null);
    }

    public ScaleProgressView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ScaleProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomRectProg);
        mProgressColor = ta.getColor(R.styleable.CustomRectProg_CRP_mainColor, Color.GRAY);
        mCuttrnyColor = ta.getColor(R.styleable.CustomRectProg_CRP_currentProColor, Color.RED);
        mtextColor = ta.getColor(R.styleable.CustomRectProg_CRP_textColor, Color.BLACK);
        mSegmentColor = ta.getColor(R.styleable.CustomRectProg_CRP_segmentColor, Color.GREEN);
        mMaxValue = ta.getInt(R.styleable.CustomRectProg_CRP_srogMaxValue, 100);
        mCurrentValue = ta.getInt(R.styleable.CustomRectProg_CRP_currentValue, 0);
        init();
    }

    private void init() {
        Intervals = new ArrayList<>();
        //默认最大值为100
        mMaxValue = 100;
        mProgressHeight = 20;
        mProbgPaint = new Paint();
        mProbgPaint.setAntiAlias(true);
        mProbgPaint.setStyle(Paint.Style.FILL);
        mProbgPaint.setColor(mProgressColor);
        mProbgPaint.setStrokeWidth(5); //设置paint的外边框

        mProPaint = new Paint();
        mProPaint.setAntiAlias(true);
        mProPaint.setStyle(Paint.Style.FILL);
        mProPaint.setColor(mCuttrnyColor);
        mProPaint.setStrokeWidth(5); //设置paint的外边框

        mMarkerPaint = new Paint();
        mMarkerPaint.setAntiAlias(true);
        mMarkerPaint.setStyle(Paint.Style.FILL);
        mMarkerPaint.setColor(mtextColor);
        mMarkerPaint.setStrokeWidth(5); //设置paint的外边框
        mMarkerPaint.setTextSize(30);

        mSegmentPaint = new Paint();
        mSegmentPaint.setAntiAlias(true);
        mSegmentPaint.setStyle(Paint.Style.FILL);
        mSegmentPaint.setColor(mSegmentColor);
        mSegmentPaint.setStrokeWidth(5); //设置paint的外边框
        mSegmentPaint.setTextSize(30);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int minheight = UIUtil.dp2px(getContext(), 26);
        int minWidth = UIUtil.dp2px(getContext(), 300);
        if ((widthMode == MeasureSpec.AT_MOST && heightMode == MeasureSpec.AT_MOST)||(widthMode == MeasureSpec.UNSPECIFIED && heightMode == MeasureSpec.UNSPECIFIED)) {
            setMeasuredDimension(minWidth, minheight);
        } else if (widthMode == MeasureSpec.AT_MOST || widthMode == MeasureSpec.UNSPECIFIED) {
            setMeasuredDimension(minWidth, height);
        } else if (heightMode == MeasureSpec.AT_MOST || height == MeasureSpec.UNSPECIFIED) {
            setMeasuredDimension(width, minheight);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        //获取开始绘制的x坐标
        mLeftX = getPaddingStart();
        //结束位置
        mRightX = getWidth() - mLeftX - getPaddingRight();
        //获取开始绘制的左上角位置
        mLeftY = getPaddingTop();
        //获取绘制的右下角y坐标
        mRightY = mLeftY + getHeight();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //设置背景
//        setBackgroundColor(mBgColor);
        //progress的长度
        int width = mRightX - mLeftX;
        //首先画出矩形
        drawRect(canvas, width);
        //画文本
        drawText(canvas, width);
        //画每个分段
        drawSegment(canvas, width);
    }

    private void drawText(Canvas canvas, int width) {
        if (mPositionDesc != null && mPositionDesc.length > 0) {
            //根据文本个数，平均空间，获取点的位置
            float first = mMarkerPaint.measureText(mPositionDesc[0]) / 2;
            float last = mMarkerPaint.measureText(mPositionDesc[mPositionDesc.length - 1]) / 2;
            //将进度条的总长度/（文本个数-1） = 每个间隔长度
            int desSpace = (int) ((width * 1.0 - first - last) / (mPositionDesc.length - 1) + 0.5);
            //文本高度
            int textHeight = (int) (mMarkerPaint.getFontMetrics().descent - mMarkerPaint.getFontMetrics().ascent);
            for (int i = 0; i < mPositionDesc.length; i++) {
                String text = mPositionDesc[i];
                int startX = mLeftX + i * desSpace;
                int y = mLeftY + mProgressHeight + 2 + textHeight;
                //由于在首位都空出了距离，所以第一个标记和最后一个标记需要处理开始位置
                float textWidth = mMarkerPaint.measureText(mPositionDesc[i]);
                startX = i != 0 ? (int) (startX + first) : startX;
                canvas.drawRect(startX, mLeftY - mProgressHeight / 2, startX, y, mMarkerPaint);
                Log.d("stasrtX", "------------------startX" + startX);
                startX = i == 0 ? startX : (int) (startX - textWidth / 2);
                Log.d("stasrtX", "2------------------startX" + startX);
                Log.d("stasrtX", "-----------------------------------");
                canvas.drawText(text, startX, y, mMarkerPaint);
            }
        }
    }

    private void drawRect(Canvas canvas, int width) {
        int left = mLeftX;
        int right = mRightX;
        int top = mLeftY;
        int bottom = top + mProgressHeight;
        //若是有文本，需要在左右两边空出距离，以对其文本中心
        if (mPositionDesc != null && mPositionDesc.length > 0) {
            float textWidth = mMarkerPaint.measureText(mPositionDesc[0]) / 2;
            left += textWidth;
            float rTextWidth = mMarkerPaint.measureText(mPositionDesc[mPositionDesc.length - 1]) / 2;
            right -= rTextWidth;
            width = (int) (width - textWidth - rTextWidth + 0.5);
        }
        if (mCurrentValue <= mMaxValue && mCurrentValue >= 0) {
            //计算出当前进度所占位置坐标 完成
            int mCompleteWidth = (int) (((mCurrentValue * 1.0) / (mMaxValue * 1.0)) * width + 0.5);
            canvas.drawRect(left, top, left + mCompleteWidth, bottom, mProPaint);
            //画剩余部分
            canvas.drawRect(left + mCompleteWidth, top, right, bottom, mProbgPaint);
        } else {
            canvas.drawRect(left, top, right, bottom, mProbgPaint);
        }
    }


    private void drawSegment(Canvas canvas, int width) {
        if (mSegmentProgressList != null && mSegmentProgressList.size() > 0) {
            int left = mLeftX;
            int right = mRightX;
            int top = mLeftY;
            int bottom = top + mProgressHeight;
            //若是有文本，需要在左右两边空出距离，以对其文本中心

            if (mPositionDesc != null && mPositionDesc.length > 0) {
                float textWidth = textWidth = mMarkerPaint.measureText(mPositionDesc[0]) / 2;
                left += textWidth;
                float rTextWidth = mMarkerPaint.measureText(mPositionDesc[mPositionDesc.length - 1]) / 2;
                right -= rTextWidth;
                width = (int) (width - textWidth - rTextWidth + 0.5);
            }
            for (ISegmentProgress bean : mSegmentProgressList) {
                if (bean.getStartPosition() < mMaxValue) {
                    int start = (int) ((((bean.getStartPosition()) * 1.0) / (mMaxValue * 1.0)) * width + 0.5);
                    int end = (int) ((((bean.getEndPosition()) * 1.0) / (mMaxValue * 1.0)) * width + 0.5);
                    end = end <= width ? end : width;
                    canvas.drawRect(left + start, top, left + end, bottom, mSegmentPaint);
                }
            }
        }
    }


    public void setSegmentProgressList(List<? extends ISegmentProgress> segmentProgressList) {
        mSegmentProgressList = segmentProgressList;
        invalidate();
    }

    public void setMaxValue(int maxValue) {
        mMaxValue = maxValue;
        invalidate();
    }


    public void setCurrentValue(int currentValue) {
        mCurrentValue = currentValue;
        invalidate();
    }

    /**
     * @param value               当前进度
     * @param segmentProgressList 分段刻度
     */
    public void setValue(int value, List<? extends ISegmentProgress> segmentProgressList) {
        mSegmentProgressList = segmentProgressList;
        mCuttrnyColor = value;
        invalidate();
    }

    /**
     * @param maxValue            最大值
     * @param value               当前进度
     * @param segmentProgressList 分段刻度
     */
    public void setValue(int maxValue, int value, List<? extends ISegmentProgress> segmentProgressList) {
        mSegmentProgressList = segmentProgressList;
        mCurrentValue = value;
        mMaxValue = maxValue;
        invalidate();
    }
}
